package com.yanqu.road.server.manger.activity;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.task.args.CommonActivityUnionRiseRankArgs;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.ActivityMallModule;
import com.yanqu.road.server.gameplayer.module.activity.common.CommonActivityModule;
import com.yanqu.road.server.logic.activity.common.CommonActivityData;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.CommonActivityPb;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


public class CommonActivityMgr extends TempMgr {

    private static Logger logger = LogManager.getLogger(CommonActivityMgr.class.getName());

    private static Map<Integer, ActivityInfo> activityInfoMap = new ConcurrentHashMap<>();

    private static Map<Integer, Map<Integer, List<ActivityConditionInfo>>> activityConditionInfoMap = new ConcurrentHashMap<>();

    private static Map<Integer, Map<Integer, MallInfo>> activityMallInfoMap = new ConcurrentHashMap<>();

    @Override
    public boolean stop() {
        return false;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean save() {
        return true;
    }

    @Override
    public boolean reloadData() {
        activityInfoMap = new ConcurrentHashMap<>();
        activityConditionInfoMap = new ConcurrentHashMap<>();
        activityMallInfoMap = new ConcurrentHashMap<>();
        return true;
    }

    public static void reloadActivityData(Map<Integer, ActivityInfo> selectActivityInfoMap, Map<Integer, Map<Integer, MallInfo>> selectActivityMallInfoMap,
                                          Map<Integer, Map<Integer, List<ActivityConditionInfo>>> selectActivityConditionInfoMap){
        logger.info("reload CommonActivity start");
        Map<Integer, ActivityInfo> inTimeActivityMap = new ConcurrentHashMap<>();
        for(ActivityInfo activityInfo : selectActivityInfoMap.values()){
            if(ActivityMgr.activityInShowTime(activityInfo)){
                //设置一下万能活动道具，再放到内存
                activityInfo.setCommonActivityItemList(StringUtils.stringToIntegerList(activityInfo.getParam3(), "\\|"));
                inTimeActivityMap.put(activityInfo.getActivityId(), activityInfo);
                if(null != selectActivityConditionInfoMap.get(activityInfo.getActivityId())) {
                    activityConditionInfoMap.put(activityInfo.getActivityId(), selectActivityConditionInfoMap.get(activityInfo.getActivityId()));
                }
                if(null != selectActivityMallInfoMap.get(activityInfo.getActivityId())){
                    activityMallInfoMap.put(activityInfo.getActivityId(), selectActivityMallInfoMap.get(activityInfo.getActivityId()));
                }
            }
        }
        activityInfoMap = inTimeActivityMap;
        CommonActivityRankMgr.reloadRank();
        playerReloadActivity();
        logger.info("reload CommonActivity end");
    }

    private static void playerReloadActivity() {
        List<GamePlayer> playerList = GamePlayerMgr.getCopyPlayerList();
        playerList.forEach(action -> {
            action.getModule(CommonActivityModule.class).addNewActivity();
            action.getModule(CommonActivityModule.class).syncActivity();
            action.getModule(ActivityMallModule.class).syncMallBuyCount();
            action.getModule(ActivityMallModule.class).syncActivityScore();
        });
    }

    public static MallInfo getMallInfo(int mallId){
        MallInfo mallInfo;
        for(ActivityInfo activityInfo : activityInfoMap.values()){
            Map<Integer, MallInfo> mallInfoMap = activityMallInfoMap.get(activityInfo.getActivityId());
            if(null != mallInfoMap){
                mallInfo = mallInfoMap.get(mallId);
                if(null != mallInfo){
                    return mallInfo;
                }
            }
        }
        return null;
    }

    /**
     * 是否是跨服万能活动
     */
    public static boolean isCrossCommonActivity(int activityId){
        ActivityInfo activityInfo = getOpenCommonActivityInfo(activityId);
        return null != activityInfo && activityInfo.getType() == eActivityType.CrossCommonActivity.getValue();
    }

    public static ActivityInfo getOpenCommonActivityInfo(int activityId){
        ActivityInfo activityInfo = activityInfoMap.get(activityId);
        if(ActivityMgr.activityInShowTime(activityInfo)){
            return activityInfo;
        }
        return null;
    }

    public static ActivityInfo getInTimeCommonActivityInfo(int activityId){
        ActivityInfo activityInfo = activityInfoMap.get(activityId);
        if(ActivityMgr.activityInTime(activityInfo)){
            return activityInfo;
        }
        return null;
    }

    /**
     * 获取万能活动
     */
    public static List<ActivityInfo> getAllOpenCommonActivityInfoList() {
        List<ActivityInfo> dataList = new ArrayList<>();
        for(ActivityInfo activityInfo : activityInfoMap.values()) {
            if (ActivityMgr.activityInShowTime(activityInfo)) {
                dataList.add(activityInfo);
            }
        }
        return dataList;
    }

    /**
     * 根据类型获取万能活动
     */
    public static List<ActivityInfo> getOpenCommonActivityInfoList(int activityType) {
        List<ActivityInfo> dataList = new ArrayList<>();
        for(ActivityInfo activityInfo : activityInfoMap.values()) {
            if (ActivityMgr.activityInShowTime(activityInfo) && activityInfo.getType() == activityType) {
                dataList.add(activityInfo);
            }
        }
        return dataList;
    }

    /**
     * 根据类型获取万能活动
     */
    public static List<Integer> getOpenCommonActivityIdList(int activityType){
        List<Integer> dataList = new ArrayList<>();
        for(ActivityInfo activityInfo : activityInfoMap.values()) {
            if (ActivityMgr.activityInShowTime(activityInfo) && activityInfo.getType() == activityType) {
                dataList.add(activityInfo.getActivityId());
            }
        }
        return dataList;
    }

    /**
     * 获取万能活动
     */
    public static List<ActivityInfo> getAllInTimeCommonActivityInfoList() {
        List<ActivityInfo> dataList = new ArrayList<>();
        for(ActivityInfo activityInfo : activityInfoMap.values()) {
            if (ActivityMgr.activityInTime(activityInfo)) {
                dataList.add(activityInfo);
            }
        }
        return dataList;
    }

    /**
     * 根据类型获取万能活动
     */
    public static List<ActivityInfo> getInTimeCommonActivityInfoList(int activityType) {
        List<ActivityInfo> dataList = new ArrayList<>();
        for(ActivityInfo activityInfo : activityInfoMap.values()) {
            if (ActivityMgr.activityInTime(activityInfo) && activityInfo.getType() == activityType) {
                dataList.add(activityInfo);
            }
        }
        return dataList;
    }

    public static List<ActivityConditionInfo> getCommonActivityConditionInfoList(int activityId, int type){
        if(activityConditionInfoMap.containsKey(activityId)){
            return activityConditionInfoMap.get(activityId).get(type);
        }
        return null;
    }

    public static ActivityConditionInfo getCommonActivityConditionInfoByRank(int activityId, int type, int rank) {
        List<ActivityConditionInfo> dataList = getCommonActivityConditionInfoList(activityId, type);
        if(null != dataList) {
            for (ActivityConditionInfo conditionInfo : dataList) {
                if (rank >= conditionInfo.getParamList().get(0).intValue() && rank <= conditionInfo.getParamList().get(1).intValue()) {
                    return conditionInfo;
                }
            }
        }
        return null;
    }

    public static ActivityConditionInfo getCommonActivityConditionInfo(int activityId, int conditionId) {
        Map<Integer, List<ActivityConditionInfo>> dataMap = getCommonActivityConditionInfoMap(activityId);
        if(null != dataMap) {
            for (List<ActivityConditionInfo> conditionInfoList : dataMap.values()) {
                for (ActivityConditionInfo conditionInfo : conditionInfoList) {
                    if (conditionId == conditionInfo.getConditionId()) {
                        return conditionInfo;
                    }
                }
            }
        }
        return null;
    }

    public static int getMaxRankByType(int activityId, int type) {
        int maxRank = 0;
        List<ActivityConditionInfo> dataList = getCommonActivityConditionInfoList(activityId, type);
        if(null != dataList) {
            for (ActivityConditionInfo conditionInfo : dataList) {
                if (conditionInfo.getParamList().get(1).intValue() > maxRank) {
                    maxRank = conditionInfo.getParamList().get(1).intValue();
                }
            }
        }
        return maxRank;
    }

    public static Map<Integer, List<ActivityConditionInfo>> getCommonActivityConditionInfoMap(int activityId) {
        return activityConditionInfoMap.get(activityId);
    }

    public static CommonActivityProto.CommonActivityDetailMsg.Builder getCommonActivityDetailMsg(Map<Integer, Map<Integer, CommonActivityData>> activityDataMap, String language){
        CommonActivityProto.CommonActivityDetailMsg.Builder detailMsg = CommonActivityPb.parseCommonActivityDetailMsg(activityDataMap, activityInfoMap,
                activityConditionInfoMap, activityMallInfoMap, language);
        return detailMsg;
    }

    public static void addCommonActivityScore(GamePlayer player, int goodsId, int addValue, long oldValue, long nowValue, eLogMoneyType master, eLogMoneyType son){
        List<ActivityInfo> activityInfoList = getAllInTimeCommonActivityInfoList();
        for(ActivityInfo activityInfo : activityInfoList) {
            if (goodsId == Integer.parseInt(activityInfo.getParam2())) {
                if(master == eLogMoneyType.Activity && son == eLogMoneyType.CommonActivityUseGoodsReward) {//使用活动道具才加分数
                    player.notifyListener(eGamePlayerEventType.CommonActivityScoreRiseRank.getValue(),
                            new CommonActivityUnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_NORMAL, BigInteger.valueOf(addValue),
                                    player.getUserInfo().getUnionUid(), activityInfo.getChildType()));
                    //记录活动积分
                    player.getModule(ActivityMallModule.class).addScore(activityInfo.getActivityId(), goodsId, addValue);
                }
                LogMgr.addLogCommonActivityScore(player.getUserId(), activityInfo.getActivityId(), goodsId, true, addValue, oldValue, nowValue,
                        master.getValue(), son.getValue());
                break;
            }
        }
    }

    public static void removeCommonActivityScore(GamePlayer player, int goodsId, int removeValue, long oldValue, long nowValue, eLogMoneyType master, eLogMoneyType son){
        List<ActivityInfo> activityInfoList = getAllOpenCommonActivityInfoList();
        for(ActivityInfo activityInfo : activityInfoList) {
            if (goodsId == Integer.parseInt(activityInfo.getParam2())) {
                LogMgr.addLogCommonActivityScore(player.getUserId(), activityInfo.getActivityId(), goodsId, false, removeValue, oldValue, nowValue,
                        master.getValue(), son.getValue());
                break;
            }
        }
    }

    /**
     * 获取万能活动的所有商店
     * @param activityId
     * @return
     */
    public static Map<Integer, MallInfo> getMallInfoMap(int activityId) {
        return activityMallInfoMap.get(activityId);
    }

    /**
     * @param activityId
     * @param Item
     * @return
     */
    public static MallInfo getMallInfoByItem(int activityId, String Item) {
        Map<Integer, MallInfo> mallInfoMap = activityMallInfoMap.get(activityId);
        if (mallInfoMap == null || mallInfoMap.isEmpty()) {
            return null;
        }
        for (MallInfo mallInfo : mallInfoMap.values()) {
            if (mallInfo.getReward().equals(Item)) {
                return mallInfo;
            }
        }
        return null;
    }


}
